<?php
namespace app\administrative\controller;
use app\common\model\LogModel;
use think\Db;
use com\IpLocation;
/**
 * 
 * @author yq
 * 系统日志
 *
 */
class Log extends Base{
    
    /**
     * 日志列表
     */
    public function index(){
        $log = new LogModel();
        $admin_name = input('param.admin_name');
        $starting_time = input('param.starting_time');
        $end_time  = input('param.end_time');
        if(strtotime($starting_time) > strtotime($end_time)){
            return json(['code'=>2,msg=>'开始时间不能大于结束时间','data'=>'']);
        }
        //搜索条件
        if(!empty($admin_name)){
            $where['admin_name']  = $admin_name;
        }
        if(!empty($starting_time) && !empty($end_time)){
            $where['add_time'] = ['between', [strtotime($starting_time), strtotime($end_time)+(60*60*24-1)]];//终止到当天23:59:59
        }else if(!empty($starting_time)){
            $where['add_time'] = ['>=',strtotime($starting_time)];
        }else if(!empty($end_time)){
            $where['add_time'] = ['<=', strtotime($end_time)+(60*60*24-1)];
        }
        
        //判断如果是超管则查询当前行政中心下所有的管理员帐号
        if(session($this->set['db_name'].'is_admin') != 1){
            $where['admin_id'] = session($this->set['db_name'].'uid');
        }else{
            //获取当前行政中心下所有管理员ID
            $id = Db::name($this->set['db_name'])->field('id')->where(['a_id'=>session($this->set['db_name'].'a_id')])->select();
            foreach ($id as $k => $v){
                $userId[] = $v['id'];
            }
            $where['admin_id'] = ['in',implode(',',$userId)];
        }
        //页数
        $limit = config('pagesize');
        $page = input('param.page') ? input('param.page'):1;
        $count = $log->getCount($where, $this->set['model'].'_log');//计算总页面
        $allpage = intval(ceil($count / $limit));//总页数
        $page = $page > $allpage ? $allpage:$page;
        $lists = $log->getAll($where, $page, $limit, 'add_time desc',  $this->set['model'].'_log');
        $Ip = new IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
        foreach($lists as $k=>$v){
            $lists[$k]['add_time']=date('Y-m-d H:i:s',$v['add_time']);
            $lists[$k]['ipaddr'] = $Ip->getlocation($lists[$k]['ip']);
        } 
        
        $this->assign([
            'page'=>$page,
            'allpage'=>$allpage,
            'count'=>$count,
            'admin_name'=>$admin_name,
            'starting_time'=>$starting_time,
            'end_time'=>$end_time
            
        ]);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }
    
}